package multiThreaded;

import java.util.concurrent.ArrayBlockingQueue;

/**
 * Page buffer for the web spider.
 * @author DoubleS
 * @version 1.0
 */
public class PageBuffer
{
  /**
   * Default max number of web pages can be buffered.
   */
  public static final int MAX_PAGE_BUFFER = 50;
  
  /**
   * The array that stores buffered web pages.
   */
  private ArrayBlockingQueue<WebPage> my_webpage_buffer;
  
  /**
   * Construct an empty PageBuffer.
   */
  public PageBuffer()
  {
    my_webpage_buffer = new ArrayBlockingQueue<WebPage>(MAX_PAGE_BUFFER);
  }
  
  /**
   * Add a web page into the page buffer.
   * @param the_page The web page.
   */
  public void addToPageBuffer(final WebPage the_page)
  {
    try
    {
      my_webpage_buffer.put(the_page);
    }
    catch (final InterruptedException e)
    {
      e.printStackTrace();
    }
  }
  
  /**
   * 
   * @return A web page from the page buffer.
   */
  public WebPage retrievePageFromBuffer()
  {
    WebPage the_page = null;
    
    try
    {
      the_page = my_webpage_buffer.take();
    }
    catch (final InterruptedException e)
    {
      e.printStackTrace();
    }
    
    return the_page;
  }
  
  /**
   * 
   * @return True if this buffer is empty. False otherwise.
   */
  public boolean isEmtpy()
  {
    return my_webpage_buffer.isEmpty();
  }
}
